package vip.xiaonuo.bi.modular.datasource.doris;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.bi.modular.datasource.mapper.EXampleTblMapper;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static vip.xiaonuo.bi.core.datasource.util.DorisUtils.insertListDataBatch;

/**
 * 数据源连接信息Service接口实现类
 *
 * @author 顾育司
 * @date  2023/12/17 13:14
 **/
@DS("doris")
@Service
@Slf4j
public class DorisServiceImple  extends ServiceImpl<EXampleTblMapper, ExampleTbl> implements DorisTbService {

    @Autowired
    @Qualifier("dorisJdbcTemplate")
    JdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void exeDDLsql(String sql){
        baseMapper.exeDDLsql(sql);
    }

    @Override
    public List<List<String>> exeQuerySql1(String sql) {
        log.info("doris执行sql：" + sql);
        List<List<String>> result =  jdbcTemplate.query(sql, new ResultSetExtractor<List<List<String>>>() {
            @Override
            public List<List<String>> extractData(ResultSet rs) throws SQLException, DataAccessException {
                ResultSetMetaData metaData = rs.getMetaData();
                int columnCount = metaData.getColumnCount();

                // 获取列名
                List<String> columnNames = new ArrayList<>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnName(i);
                    columnNames.add(columnName);
                }

                // 构造结果集
                List<List<String>> rows = new ArrayList<>();
                while (rs.next()) {
                    List<String> row = new ArrayList<>();
                    for (int i = 1; i <= columnCount; i++) {
                        Object value = rs.getObject(i);
                        row.add(value==null?null:value.toString());
                    }
                    rows.add(row);
                }
                // 在结果集中添加列名行
                rows.add(0, columnNames);

                return rows;
            }
        });
        System.out.println("doris:"+result);
        return result;
    }

    @Override
    public void exeInsertData(List<List<String>> dataList, Map<String, String> columnstype, String tableName, List<String> columns) {
        insertListDataBatch(jdbcTemplate,dataList,columnstype,tableName,columns);
    }




    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public List<Map<String, Object>> exeQuerySql(String sql) {
        List<Map<String, Object>> result = baseMapper.exeQuerySql(sql);
        return result;
    }



}
